<?php
/*
 * Created on Sep 6, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("impl/CategoryImpl.php");
include_once("impl/MerchantImpl.php");

include_once("impl/UploadFileImpl.php");

include_once("utils/FileUtils.php");
include_once("utils/StringUtils.php");
include_once("constant/GlobalConstant.php");

include_once("domain/AdvertiserDomain.php");

class AdvertiserImpl
{
	private $DBTag;
	
	private $merImpl;
	private $catImpl;
	private $ufImpl;
	private $fileUtils;
	
	
	function AdvertiserImpl($DBTag)
	{
		$this->DBTag 	= $DBTag;
		
		$this->merImpl 		= new MerchantImpl($DBTag);
		$this->catImpl 		= new CategoryImpl($DBTag);
		$this->ufImpl 		= new UploadFileImpl($DBTag);
		$this->fileUtils	= new FileUtils();
		
	}
	
	public function updateMerchantByGooglAdvertiser($oMer, $adv)
	{
		$nameId = $oMer->getNameId();
		
		if(empty($nameId))
			return;
			
		$nameFirstLetter 		= strtolower($nameId[0]);
		
		if(!preg_match('/^[a-zA-Z]+$/', $nameFirstLetter)){
			$nameFirstLetter = '#';
		}
		
		// update merchant data
		$oMer->setDisplayURL($adv->getSiteUrl());
		$oMer->setDescript($adv->getDescription());
		
		$oMer->setCSPhone($adv->getContactPhone());		
		$oMer->setCSEmail($adv->getContactEmail());
		
		$oMer->setNameFirstLetter($nameFirstLetter);
		
		$this->merImpl->edit($oMer);
		
		$this->merImpl->addAdvertiserCategory(GlobalConstant::SOURCE_ID_GOOGLE, $adv->getId(), $adv->getCategory());
		$this->merImpl->addAlias($oMer->getId(), $oMer->getName());
		
		// is product feed
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '4', $adv->getProductFeedsEnabled());
		
		// google advertiser Id
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '5', $adv->getId());
		
		// Advertiser 3 month EPC
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '18', $adv->getEpcNinetyDayAverageAmount());
		
		// Advertiser 7 day EPC
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '19', $adv->getEpcSevenDayAverageAmount());
		
		// Commission Duration
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '20', $adv->getCommissionDuration());
		
		$categoryId = $this->getCategoryId($adv->getCategory());			
						
		if(!empty($categoryId)){
			$this->merImpl->addCategory($oMer->getId(), $categoryId);
		}
	}
	
	public function addMerchantByGooglAdvertiser($adv)
	{
		// get name id of merchant
		$nameId	= StringUtils::getCharNumReplace($adv->getName(), '_');
		$nameId	= trim($nameId);
		$nameId	= trim($nameId, '_');
		
		// get first letter of merchant name
		$nameFirstLetter 		= strtolower($nameId[0]);
		
		if(!preg_match('/^[a-zA-Z]+$/', $nameFirstLetter)){
			$nameFirstLetter = '#';
		}		
		
		// add new merchant
		$oMer = new MerchantDomain();
	
		$oMer->setName($adv->getName());
		$oMer->setNameId($nameId);
		$oMer->setDisplayURL($adv->getSiteUrl());
		$oMer->setDescript($adv->getDescription());
		$oMer->setFeatured('0');
		
		$oMer->setCSPhone($adv->getContactPhone());		
		$oMer->setCSEmail($adv->getContactEmail());
		
		$oMer->setStatus('1');
		$oMer->setNameFirstLetter($nameFirstLetter);
		
		$this->merImpl->add($oMer);
		
		echo "name id: " . $nameId;
		echo "\n";
		
		echo "merchant id: " . $oMer->getId();
		echo "\n";	
		
		if($oMer->getId() > 0){
			echo "----------------- insert new merchant ------------------------------";		
			echo "\n";
			
			echo "name: " . $adv->getName();
			echo "\n";
			
			echo "11 merchant id: " . $oMer->getId();
			echo "\n";		
			
			$this->merImpl->addSource($oMer->getId(), GlobalConstant::SOURCE_ID_GOOGLE);
			
			$this->merImpl->addAdvertiserCategory(GlobalConstant::SOURCE_ID_GOOGLE, $adv->getId(), $adv->getCategory());
			$this->merImpl->addAlias($oMer->getId(), $oMer->getName());
			
			// is product feed
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '4', $adv->getProductFeedsEnabled());
			
			// google advertiser Id
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '5', $adv->getId());
			
			// Advertiser 3 month EPC
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '18', $adv->getEpcNinetyDayAverageAmount());
			
			// Advertiser 7 day EPC
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '19', $adv->getEpcSevenDayAverageAmount());
			
			// Commission Duration
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_GOOGLE, $oMer->getId(), '20', $adv->getCommissionDuration());
			
			$categoryId = $this->getCategoryId($adv->getCategory());			
						
			echo "category id: " . $categoryId;
			echo "\n";			
			
			if(!empty($categoryId)){
				$this->merImpl->addCategory($oMer->getId(), $categoryId);
			}	
			
			$this->addLogo($adv, $oMer);			
		}else{
			echo "insert merchant error, message: " . $this->merImpl->getMessage();
			echo "\n";
			
		}
		
		if($oMer->getId() > 0)
			return $oMer;
		else
			return false;
	}
	
	public function addMerchantByCjAdvertiser($adv)
	{
		$fileAdvertiserName = StringUtils::getStartValue(' - ', $adv->advertiserName);
		
		// get name id of merchant
		$nameId	= StringUtils::getCharNumReplace($fileAdvertiserName, '_');
		$nameId	= trim($nameId);
		$nameId	= trim($nameId, '_');
		
		// get first letter of merchant name
		$nameFirstLetter 		= strtolower($nameId[0]);
		
		if(!preg_match('/^[a-zA-Z]+$/', $nameFirstLetter)){
			$nameFirstLetter = '#';
		}		
		
		// add new merchant
		$oMer = new MerchantDomain();
	
		$oMer->setName($fileAdvertiserName);
		$oMer->setHeadline($adv->advertiserName);
		$oMer->setNameId($nameId);
		$oMer->setDisplayURL($adv->programUrl);
		$oMer->setDescript('');
		$oMer->setFeatured('0');
		
		$oMer->setCSPhone('');		
		$oMer->setCSEmail('');
		
		$oMer->setStatus('1');
		$oMer->setNameFirstLetter($nameFirstLetter);
		
		$this->merImpl->add($oMer);
		
		echo "name id: " . $nameId;
		echo "\n";
		
		echo "merchant id: " . $oMer->getId();
		echo "\n";	
		
		if($oMer->getId() > 0){
			echo "----------------- insert new merchant ------------------------------";		
			echo "\n";
			
			$this->merImpl->addSource($oMer->getId(), GlobalConstant::SOURCE_ID_CJ);
			
			$category = $adv->getChildCategory();
			
			if(!empty($category))
				$this->merImpl->addAdvertiserCategory(GlobalConstant::SOURCE_ID_CJ, $adv->advertiserId, $category);
			
			$this->merImpl->addAlias($oMer->getId(), $oMer->getName());
			
			// google advertiser Id
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '5', $adv->advertiserId);
			
			// Advertiser 3 month EPC
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '18', $adv->threeMonthEpc);
			
			// Advertiser 7 day EPC
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '19', $adv->sevenDayEpc);
			
			// network rank
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '69', $adv->networkRank);
			
			// default Commission
			$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '70', $adv->commission);
			
			$categoryId = $this->getCategoryId($category);
						
//			echo "category id: " . $categoryId;
//			echo "\n";			
			
			if(!empty($categoryId)){
				$this->merImpl->addCategory($oMer->getId(), $categoryId);
			}else{
				echo "Warinning!!!!!!!!!!!!!!! not found category by " . $category;
				echo "\n";	
			}	
		}else{
			echo "insert merchant error, message: " . $this->merImpl->getMessage();
			echo "\n";
			
		}
		
		if($oMer->getId() > 0)
			return $oMer;
		else
			return false;
	}
	
	public function updateMerchantByCjAdvertiser($oMer, $adv)
	{
		
		$this->merImpl->addSource($oMer->getId(), GlobalConstant::SOURCE_ID_CJ);
			
		$category = $adv->getChildCategory();
		
		if(!empty($category))
			$this->merImpl->addAdvertiserCategory(GlobalConstant::SOURCE_ID_CJ, $adv->advertiserId, $category);
		
		$this->merImpl->addAlias($oMer->getId(), $oMer->getName());
		
		// google advertiser Id
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '5', $adv->advertiserId);
		
		// Advertiser 3 month EPC
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '18', $adv->threeMonthEpc);
		
		// Advertiser 7 day EPC
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '19', $adv->sevenDayEpc);
		
		// network rank
		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '69', $adv->networkRank);
		
//		// default Commission
//		$this->merImpl->addAttribute(GlobalConstant::SOURCE_ID_CJ, $oMer->getId(), '70', $adv->commission);
		
		$categoryId = $this->getCategoryId($category);
					
//		echo "category id: " . $categoryId;
//		echo "\n";			
		
		if(!empty($categoryId)){
			$this->merImpl->addCategory($oMer->getId(), $categoryId);
		}else{
			echo "warnning!!!!!!!!!!!!!!!!! not found category by " . $category;
			echo "\n";	
		}
	}
	
	public function addLogo($adv, $oMer)
	{
		//		echo "get merchant logo.";
		//		echo "\n";
		
		$get_file = @file_get_contents($adv->getLogoUrl());
		
		if($get_file){
			$tmpfname 	= tempnam("../tmp/", "mer_logo_");
			$fp 		= @fopen($tmpfname, "w");
			
			@fwrite($fp, $get_file);
			@fclose($fp);
			
			$imageinfo 				= getimagesize($tmpfname);
			$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
			$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
					
			$fileSize	= filesize($tmpfname);
			
			$width		= $imageinfo[0];
			$height		= $imageinfo[1];
			
			$md5		= md5(file_get_contents($tmpfname));
			$extName	= $image_extend_name;

			if(!empty($extName)){
				$oUpf		= $this->ufImpl->getByMd5($md5);
				
				if(is_object($oUpf)){
					$imageid	= $oUpf->getId();
					if(empty($imageid)){
						$imageid = $this->ufImpl->add($extName, $md5, $width, $height, $fileSize);
						
						echo "add new merchant logo image id = $imageid ";
						echo "\n";					
						
						$imageName 	= $imageid . '.' . strtolower($extName);
						
						$target 	= $this->fileUtils->getFullPathByID(IMAGE_PATH_LOGO, $imageid, $imageName);
						
						echo "target = $target";
						echo "\n";
						
						@copy($tmpfname, $target);
					}
					
					if(!empty($imageid))
						$this->merImpl->addLogo($oMer->getId(), $imageid);
				}
				
				
			}
			
			unlink($tmpfname);
		}else{
			echo "merchant id: " . $oMer->getId();
			echo "\n";		
			
			echo "advertiser name: " . $adv->getName();
			echo "\n";
			
			echo "get logo failed. logo url = " . $adv->getLogoUrl();
			echo "\n";
		}
	}
	
	public function getCategoryId($adverCategory)
	{
		$categoryName = ucwords(strtolower(str_ireplace('_', ' ', $adverCategory)));
	
		$oCat = $this->catImpl->getByName($categoryName);
		
		$categoryId = '';
		
		if(is_object($oCat)){
			$categoryId = $oCat->getId();
		}else{
			$oCat = $this->catImpl->getCategoryByAlias($categoryName);
			
			if(is_object($oCat)){
				$categoryId = $oCat->getId();
			}
		}
		
		if(empty($categoryId)){
			$oCat = $this->catImpl->getCategoryByAlias($adverCategory);
			
			if(is_object($oCat)){
				$categoryId = $oCat->getId();
			}
		}
		
//		if(empty($categoryId)){
//			echo "\n";
//			echo "======================= not found category for update merchant ======================================";
//			echo "\n";
//			
//			echo "category Ailas: " . $adverCategory;
//			echo "\n";
//			
//			echo "merchant id: " . $oMer->getId();
//			echo "\n";
//			
//			echo "advertiser id: " . $adv->getId();
//			echo "\n";
//			
//			echo "category : " . $categoryName;
//			echo "\n";
//		
//			echo "\n";
//			echo "\n";
//		}
		
		
		return $categoryId;
	}
}
?>
